package com.tarena.lbs.stock.web.rpc;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.tarena.lbs.pojo.stock.dto.CouponStockDTO;
import com.tarena.lbs.pojo.stock.param.CouponStockParam;
import com.tarena.lbs.stock.api.StockApi;
import com.tarena.lbs.stock.web.service.StockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class StockApiImpl implements StockApi {
    @Autowired
    private StockService stockService;

    @Override
    public boolean initCouponStock(CouponStockParam param) {
        boolean result=false;
        try{
            result=stockService.saveCouponStock(param);
        }catch (Exception e){
            log.error("初始化优惠券:{}的库存失败",param,e);
        }
        return result;
    }

    @Override
    public boolean updateCouponNum(Integer couponId) {
        try{
            return stockService.updateCouponNum(couponId);
        }catch (Exception e){
            log.error("初始化优惠券库存失败",e);
        }
        return false;
    }

    @Override
    public List<CouponStockDTO> getCouponNum(List<Integer> ids) {
        if (CollectionUtils.isNotEmpty(ids)){
            return stockService.getCouponNum(ids);
        }
        return new ArrayList<>();
    }
}
